﻿Imports System.Data.SqlClient
Imports DTO


Public Class CuonSach_DAO
    Public Shared Function getSachTheoMaKe(ByVal make As Integer) As DataTable
        Dim dt As New DataTable
        Dim con As SqlConnection = DataProvider.ConnectToDB

        Dim cmd As New SqlCommand
        cmd.CommandText = "DauSachGetInfoFromMaKe"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = con

        Dim para As New SqlParameter("make", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = make
        cmd.Parameters.Add(para)

        Dim adapter As New SqlDataAdapter(cmd)
        adapter.Fill(dt)

        con.Close()
        Return dt
    End Function

    Public Shared Function LayTatCaThongTinTheoMaSach(ByVal masach As Integer) As DataTable
        Dim dt As New DataTable
        Dim con As SqlConnection = DataProvider.ConnectToDB

        Dim cmd As New SqlCommand
        cmd.CommandText = "getAllInfoSach"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = con

        Dim para As New SqlParameter("masach", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = masach
        cmd.Parameters.Add(para)

        Dim adapter As New SqlDataAdapter(cmd)
        adapter.Fill(dt)

        con.Close()
        Return dt
    End Function

    Public Shared Function loadInfoSach() As DataTable
        Return DataProvider.loadDataWithStore("CuonSach_DauSach_Load")
    End Function

    Public Shared Sub themSach(ByVal ds As DauSach_DTO, ByVal cs As CuonSach_DTO)
        Dim con As SqlConnection = DataProvider.ConnectToDB

        Dim cmd As New SqlCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = con
        cmd.CommandText = "CuonSach_Them"

        Dim para As New SqlParameter("tuasach", SqlDbType.NVarChar, 50)
        para.Direction = ParameterDirection.Input
        para.Value = ds.tuaSach
        cmd.Parameters.Add(para)

        para = New SqlParameter("tacgia", SqlDbType.NVarChar, 30)
        para.Direction = ParameterDirection.Input
        para.Value = ds.tacGia
        cmd.Parameters.Add(para)

        para = New SqlParameter("sotrang", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = ds.soTrang
        cmd.Parameters.Add(para)

        para = New SqlParameter("namsanxuat", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = ds.namSanXuat
        cmd.Parameters.Add(para)

        para = New SqlParameter("soluongsach", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = ds.soLuongSach
        cmd.Parameters.Add(para)

        para = New SqlParameter("tomtat", SqlDbType.NText)
        para.Direction = ParameterDirection.Input
        para.Value = ds.tomTat
        cmd.Parameters.Add(para)

        para = New SqlParameter("giatien", SqlDbType.Money)
        para.Direction = ParameterDirection.Input
        para.Value = ds.giaTien
        cmd.Parameters.Add(para)

        para = New SqlParameter("maloaisach", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = ds.maLoaiSach
        cmd.Parameters.Add(para)

        para = New SqlParameter("mads", SqlDbType.Int)
        para.Direction = ParameterDirection.Output
        cmd.Parameters.Add(para)

        para = New SqlParameter("matinhtrang", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = cs.maTinhTrang
        cmd.Parameters.Add(para)

        para = New SqlParameter("manxb", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = cs.maNXB
        cmd.Parameters.Add(para)

        para = New SqlParameter("mavitri", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = cs.maViTri
        cmd.Parameters.Add(para)

        cmd.ExecuteNonQuery()

        ds.maDauSach = cmd.Parameters("mads").Value

        FileCopy(ds.hinhAnh, "images/book/" + ds.maDauSach.ToString + ".jpg")

        con.Close()
    End Sub

End Class
